package ar.edu.itba.it.paw.web.user;

import org.apache.wicket.Component;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.validation.AbstractFormValidator;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.validation.validator.StringValidator;

import ar.edu.itba.it.paw.domain.EntityModel;
import ar.edu.itba.it.paw.domain.User;
import ar.edu.itba.it.paw.web.base.BasePage;

public class RestoreQuestionPage extends BasePage {

	private TextField<String> answer_field;
	private IModel<Boolean> visibility;
	
	public RestoreQuestionPage(User user, Boolean visible){
			
		setDefaultModel(new CompoundPropertyModel<User>(new EntityModel<User>(User.class,user)));
		visibility = new CompoundPropertyModel<Boolean>(visible);
		
		add(new Label("question",new PropertyModel<String>(getDefaultModel(), "secret_question")).add(new SimpleAttributeModifier("style", "")));
		
		add(new FeedbackPanel("feedback").add(new SimpleAttributeModifier("style", "color: #FF0000")));
		Form<RestorePasswordPage> form = new Form<RestorePasswordPage>("secretQuestionForm", new CompoundPropertyModel<RestorePasswordPage>(this)) {
			
			@Override
			public boolean isVisible() {
				return !(answer_field != null && answer_field.getInput() != null && answer_field.getInput().equals(((User)RestoreQuestionPage.this.getDefaultModelObject()).getSecret_answer()));
			}
		};
		answer_field = new TextField<String>("answer", new CompoundPropertyModel<String>(answer_field));
		answer_field.setRequired(true).add(StringValidator.maximumLength(255));
		form.add(answer_field);
		form.add(new Button("recover", new ResourceModel("recover")));
		form.add(new AbstractFormValidator() {
			
			@Override
			public void validate(Form<?> form) {
				if(answer_field.getConvertedInput() != null){
					if(!answer_field.getConvertedInput().equals(((User)RestoreQuestionPage.this.getDefaultModelObject()).getSecret_answer())){
						form.error(getString("wrongAnswer"));
					return;
					}
				}
			}
			
			@Override
			public FormComponent<?>[] getDependentFormComponents() {
				return new FormComponent[] {answer_field};

			}
		});
		add(form.setVisible(!visibility.getObject()));
		System.out.println(visibility.getObject());
		add(new Label("password",new PropertyModel<String>(getDefaultModel(), "password")){
			
			@Override
			public boolean isVisible() {
				// TODO Auto-generated method stub
				return answer_field != null && answer_field.getInput() != null && answer_field.getInput().equals(((User)RestoreQuestionPage.this.getDefaultModelObject()).getSecret_answer());
			}
		});	
	}
	
}
